package net.maku.events.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.events.vo.ActivityShareQrVO;
import net.maku.events.vo.ActivityTopVO;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.events.service.ActivityBriskWalkService;
import net.maku.events.query.ActivityBriskWalkQuery;
import net.maku.events.vo.ActivityBriskWalkVO;
import net.maku.framework.operatelog.annotations.OperateLog;
import net.maku.framework.operatelog.enums.OperateTypeEnum;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

import java.util.List;

/**
 * 健步走活动
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2025-03-22
 */
@RestController
@RequestMapping("events/activity_brisk_walk")
@Tag(name = "健步走活动")
@AllArgsConstructor
public class ActivityBriskWalkController {
    private final ActivityBriskWalkService activityBriskWalkService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('events:activity_brisk_walk:page')")
    public Result<PageResult<ActivityBriskWalkVO>> page(@ParameterObject @Valid ActivityBriskWalkQuery query) {
        PageResult<ActivityBriskWalkVO> page = activityBriskWalkService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('events:activity_brisk_walk:info')")
    public Result<ActivityBriskWalkVO> get(@PathVariable("id") Long id) {
        return Result.ok(activityBriskWalkService.getInfoById(id));
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('events:activity_brisk_walk:save')")
    public Result<String> save(@RequestBody ActivityBriskWalkVO vo) {
        activityBriskWalkService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('events:activity_brisk_walk:update')")
    public Result<String> update(@RequestBody @Valid ActivityBriskWalkVO vo) {
        activityBriskWalkService.update(vo);

        return Result.ok();
    }

    @GetMapping("/status/{id}/{status}")
    @Operation(summary = "上下架")
    @PreAuthorize("hasAuthority('events:activity_brisk_walk:status')")
    public Result<String> updateStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        activityBriskWalkService.updateStatus(id, status);
        return Result.ok();
    }

    @PostMapping("/whether/top")
    @Operation(summary = "是否置顶")
    @PreAuthorize("hasAuthority('events:activity_brisk_walk:top')")
    public Result<String> whetherTop(@RequestBody ActivityTopVO activityTopVO) {
        activityBriskWalkService.whetherTop(activityTopVO);
        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('events:activity_brisk_walk:delete')")
    public Result<String> delete(@RequestBody List<Long> idList) {
        activityBriskWalkService.deleteInfoById(idList);

        return Result.ok();
    }

    @PostMapping("create/wx/qr")
    @Operation(summary = "生成小程序二维码")
    @PreAuthorize("hasAuthority('events:activity_brisk_walk:qr')")
    public Result<String> createWxQr(@RequestBody ActivityShareQrVO activityShareQrVO) {
        return Result.ok(activityBriskWalkService.createWxQr(activityShareQrVO));
    }

    @GetMapping("export/user/ranks/{id}")
    @Operation(summary = "导出个人排行榜")
    @OperateLog(type = OperateTypeEnum.EXPORT)
    @PreAuthorize("hasAuthority('events:activity_brisk_walk:export')")
    public void exportUserRanks(@PathVariable("id") Long id) {
        activityBriskWalkService.exportUserRanks(id);
    }

    @GetMapping("export/org/ranks/{id}")
    @Operation(summary = "导出个人排行榜")
    @OperateLog(type = OperateTypeEnum.EXPORT)
    @PreAuthorize("hasAuthority('events:activity_brisk_walk:export')")
    public void exportOrgRanks(@PathVariable("id") Long id) {
        activityBriskWalkService.exportOrgRanks(id);
    }

}